<?php
/**
 * 定义 Controller_BoUploadImg 类
 *
 * @copyright Copyright (c) 2009 iTeam
 * @author sunnycoolboy
 * @package leadtone
 * @version BoProfession 2009-04-02
 */

// {{{ includes
FLEA::loadClass('Controller_BoBase');
// }}}

class Controller_BoUploadImg extends Controller_BoBase
{
	/**
     * 操作产品分类的对象
     *
     * @var Model_ArticleClasses
     */
    var $_modelClasses;
    

    /**
     * @var Model_Articles
     */
    var $_modelArticles;
    
    
    /**
     * @var access
     */
    var $access;
    
    /**
     * @var catid   类别ID  产品类别ID为47
     */
    var $catid;
	
	/**
     * 构造函数
     *
     * @return Controller_BoArticles
     */
    function Controller_BoUploadImg() {
        parent::Controller_BoBase();        
        $this->_modelClasses =& FLEA::getSingleton('Model_Categories');
        $this->_modelArticles =& FLEA::getSingleton('Model_Articles');

    }
    
    /**
     * 显示首页轮播图列表
     */
    protected function actionIndex() {

        $field = array('class_id' => $this->catid);
        
        $table =& $this->_modelArticles->getTable();
        $pk = $table->primaryKey;

        $rowset = $table->findAll($field, 'created DESC', 4);

        /*echo '<pre>';
        print_r($rowset);*/

        $this->_setBack();
        $listTitle = "首页轮播图片";
        $listSrc = LEADTONE_DIR."/index.php?controller=Default&action=Index";
        include(TPL_DIR . '/BoImgsList.php');
    }
    
    /**
     * 显示产品的图片管理器
     */
    protected function actionPicman($errorMessage = null) {
    	$catid = $this->catid;
    	$table =& $this->_modelArticles->getTable();
        $pk = $table->primaryKey;
    	$article = $this->_modelArticles->getArticle($_GET['id']);

        include(TPL_DIR . '/BoImgEdit.php');
    }
    
    /**
     * 上传缩略图
     */
    protected function actionUploadPhoto($errorMessage = null) {
    	__TRY();

        $user = $this->getUser();
		if(!$_POST['article_id'])$_POST['created'] = time(); 
		else $_POST['updated'] = time(); 
		$_POST['author'] = $user['ID'];
		$_POST['class_id'] = $this->catid;
		$_POST['title'] = $this->stripHtml($_POST['title']);//添加去除html和script标记的轮播名称
		$_POST['content'] = $this->stripHtml($_POST['link']);//添加去除html和script标记的轮播链接
        $errorMessage = $this->_uploadPicture();
        if(!$errorMessage == "" && is_array($errorMessage))$_POST['image_src'] = $errorMessage['filename'];//上传图片
        else unset($_POST['image_src']);
        
        $this->_modelArticles->saveArticle($_POST);
        $ex = __CATCH();
        if (__IS_EXCEPTION($ex)) {
            return $this->_editArticle($_POST, $ex->getMessage());
        }
        
		redirect($this->_url('Index'));
    }
    
	/**
     * 处理图片上传
     */
    protected function _uploadPicture() {

            $uploader =& FLEA::getSingleton('FLEA_Helper_FileUploader');
            /* @var $uploader FLEA_Helper_FileUploader */

            $errorMessage = "";
            // 检查上传文件是否存在
			if ($uploader->isFileExist('postfile')) {
	            // 检查文件扩展名是否是允许上传的类型
	            $file =& $uploader->getFile('postfile');
	            if (!$file->check(FLEA::getAppInf('photoFileExts'))) {
	                $errorMessage =_T('ui_p_invalid_filetype');
	                break;
	            }
	
	            // 上传图片
	            __TRY();
	            $filetype = $file->getExt();//获取图片后缀
	            $filename = $this->uploadPhoto($file,$filetype);
	            $ex = __CATCH();
	            if (__IS_EXCEPTION($ex)) {
	                $errorMessage = $ex->getMessage();
	                break;
	            }else $errorMessage['filename'] = $filename;
			}
			
            if($errorMessage!= "" && !is_array($errorMessage)){
				echo "<script>alert('".$errorMessage."');";
				echo "history.back();</script>";
				exit;
			}
		
        /*$src = FLEA::getAppInf('uploadDir') . DS . $filename;
        $img = UPLOAD_DIR. "/" .$filename;
        include(TPL_DIR . '/BoPagePicman.php');*/
        return $errorMessage;
    }
    
    /**
     * 添加图片
     *
     * @param
     * @param FLEA_Helper_UploadFile $file
     *
     * @return boolean
     */
    protected function uploadPhoto(&$file,$type) {

        $filename = 'litong-frontimg' . time() . '.' . $type;
        
        $file->move(FLEA::getAppInf('uploadDir') . DS . $filename);
        
        return $filename;
    }
}